<?php
class UpFile_ApiClient
	{
		private $data=null;
		private $url='';
		private $referer='http://localhost/index.php?a=apis.UpFile_ApiClient';
		private $auth='';
		private $authcode='';
		private $header='';
		
		public function __construct()
			{
				
			}
			
		private function setheader()
			{
				$header=1;
				if($header)
					{
						$this->header=array(
							'CLIENT_TOKEN'=>$this->auth,
							'CLIENT_CODE'=>$this->authcode
						);
					}
					else
						{
							return false;
						}
				return true;
				
			}
			
		private function setauth($auth)
			{
				if($auth)
					{
						$this->auth=$auth;
						$this->authcode=md5(substr(md5($auth),5,15).md5($auth));
					}
					else
						{
							return false;
						}
				return true;
			}
			
		//设置CURL参数，并获取数据	
		private function Api_Curl()
			{
				$time_out=300;
				$timeout=100;
				$url='';
				$referer='';
				$curl='';
				if(!$this->url && !$this->data)
					{
						return false;
					}
					try
						{
							if(!$curl=curl_init())
								{
									throw new Exception('初始化失败！');
								}
								else
									{
										
									}
						}
						catch(Exception $e)
							{
								$e->getMessage();
							}
				
				curl_setopt($curl,CURLOPT_URL,$this->url);//要转送数据到的网址（调用方）
				curl_setopt($curl,CURLOPT_REFERER,$this->referer);//发送数据的网址（被调用方）
				//curl_setopt($curl,CURLOPT_HTTPHEADER,$this->header);
				//curl_setopt($curl,CURLOPT_HEADER,'content-type:aplication/json;charset=utf-8');
				curl_setopt($curl,CURLOPT_POST,true);//推送数据的方式
				curl_setopt($curl,CURLOPT_POSTFIELDS,$this->data);//推送的数据
				curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
				curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$time_out);//连接服务器没有反应的等待时间
				curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);//接收数据时等待的时间
				
				$result_data=curl_exec($curl);//执行推送数据
				$error=curl_errno($curl);
				curl_close($curl);
				return $result_data;
			}
			
		//对外接口
		public function DoRequest($url,$auth,$data=null)
			{
				$sign_str=$this->set_sign($data);
				$sign=$this->get_sign($sign_str,$auth);
				$data['sign']=$sign;
				if($this->setauth($auth))
					{
						if($this->setheader())
							{
								$this->url=$url;
								if(!empty($data))
									{
										if(is_array($data))
											{
												foreach($data as $key => $value)
													{
														$datas.=$key.'='.urlencode($value).'&';//urlencode()
													}
													$datas=rtrim($datas,'&');
													$this->data=$datas;
													unset($datas);
											}
									}
									
								$result_data=$this->Api_Curl();
								return $result_data;
								
							}
					}
			}
			
		//类的析构函数
		public function __destruct()
			{
				
			}
			
		//循环输出多维数组的所有元素
		private function display_multi_array($array)
			{
				if(empty($array))
					{
						return false;
					}
					elseif(is_array($array))
						{
							$count=count($array);
							if($count>1)
								{
									foreach($array as $key => $value )
										{
											if(!is_array($value))
												{
													
													echo '<h3><span style="color:green;margin-right:15px;">'.$key.':</span>'.$value.'</h3>';
												}
												else
													{
														
														$this->display_multi_array($value);
													}
										}
								}
							
						}
						else
							{
								echo '<h3>'.$array.'</h3>';
							}
			}

		private function set_sign($arr)
			{
				$sign='';
				if(!is_array($arr))
					{
						return null;
					}
				ksort($arr,SORT_STRING);
				foreach($arr as $key=>$value)
					{
						if($value!=null)
							{
								$sign.=$key.(is_array($value)?$this->get_sign($value):$value);
							}
							else
								{}
						
					}
				return $sign;
			}
	
		private function get_sign($sign,$token)
			{
				return strtoupper(md5(strtoupper(md5($sign)).$token));
			}
			
	
	}
?>